package com.th.supcom.pds.impl.service.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.carefx.framework.translate.TranslatorManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.th.supcom.emr.cpoe.outprescription.service.OutPrescriptionService;
import com.th.supcom.emr.cpoe.outprescription.service.OutpPrescExecService;
import com.th.supcom.pds.domain.vo.ExecListVO;
import com.th.supcom.pds.domain.vo.PdsSupplyDetailVO;
import com.th.supcom.pds.domain.vo.PdsSupplyVO;
import com.th.supcom.pds.impl.common.utils.ServerUitls;
import com.th.supcom.pds.medicare.api.AdapterEmrPrescriptionService;

public class AdapterEmrPrescriptionServiceImpl implements AdapterEmrPrescriptionService
{
    private OutPrescriptionService m_outPrescriptionService;
    private OutpPrescExecService m_outpPrescExecService;
    private TranslatorManager m_translatorManager;

    public void setOutPrescriptionService (OutPrescriptionService outPrescriptionService)
    {

        m_outPrescriptionService = outPrescriptionService;
    }

    public void setOutpPrescExecService (OutpPrescExecService outpPrescExecService)
    {
        m_outpPrescExecService = outpPrescExecService;
    }

    public void setTranslatorManager (TranslatorManager translatorManager)
    {
        m_translatorManager = translatorManager;
    }

    /** 日志记录器. */
    public static final Logger s_logger = LoggerFactory.getLogger (AdapterEmrPrescriptionServiceImpl.class);
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    @Override
    public List <PdsSupplyVO> findApplyAndPresc (Map <String, Object> inmap)
    {
        s_logger.debug ("进入findApplyAndPresc方法,入参:");
        Map <String, Object> result = m_outPrescriptionService.findApplyAndPresc (inmap);
        @SuppressWarnings ("unchecked")
        List <Map <String, Object>> outMap = (List <Map <String, Object>>) result.get ("result");
        List <PdsSupplyVO> pdsSupplyVOList = new ArrayList <PdsSupplyVO> ();
        for (Map <String, Object> map : outMap)
        {
            PdsSupplyVO vo = new PdsSupplyVO ();

            // try
            // {
            // BeanUtils.populate (vo, map);
            // }
            // catch (IllegalAccessException e)
            // {
            // e.printStackTrace ();
            // }
            // catch (InvocationTargetException e)
            // {
            // e.printStackTrace ();
            // }
            vo.setPrescId ((Long) map.get ("id"));
            vo.setPrescSource (0);// Integer.parseInt ((String)map.get
                                  // ("prescSource"))
            vo.setVisitId ((Long) map.get ("visitId"));
            vo.setUsageId ((String) map.get ("usageId"));
            // TODO 处方类型需要统一
            vo.setPrescClassfication (Integer.valueOf (map.get ("type").toString ()));// (Integer) map.get ("type")
            vo.setPxDaily ((Integer) map.get ("pxDaily"));

            vo.setFreqCode ((String) map.get ("freqCode"));
            vo.setOrgIdExec ((String) map.get ("orgIdExec"));
            vo.setPatientId ((String) map.get ("patientId"));
            vo.setPrescType ((String) map.get ("prescType"));
            vo.setPrescNo ((String) map.get ("prescNo"));
            vo.setPatiName ((String) map.get ("patiName"));
            vo.setPatiSex ((String) map.get ("patiSex"));
            vo.setPatiAge ((String) map.get ("patiAge"));
            vo.setPxAmount ((Integer) map.get ("pxAmount"));
            vo.setPrescType ((String) map.get ("prescType"));
            vo.setOrderedBy ((String) map.get ("orgIdBeg"));
            vo.setCreateDate ((Date) map.get ("createDate"));
            vo.setOrgIdHospital ((String) map.get ("orgIdHospital"));
            vo.setOutpVisitId (Long.parseLong (map.get ("patientVisitId") + ""));
            vo.setQualifiedType ((String) map.get ("qualifiedType"));
            vo.setCheckFlag ((String) map.get ("checkFlag"));
            vo.setAmountSuggest ((Double) map.get ("amountSuggest"));
            vo.setFreqMemo ((String) map.get ("freqMemo"));
            vo.setDosageForm ((String) map.get ("dosageForm"));
            vo.setDosageFormUnit ((String) map.get ("dosageFormUnit"));
            vo.setRcptNo ((String) map.get ("rcptNo"));
            vo.setChargeDate ((Date) map.get ("chargeDate"));
            List <Map <String, Object>> list = (List <Map <String, Object>>) map.get ("execList");
            List <ExecListVO> execListVOs = new ArrayList <ExecListVO> ();
            for (Map <String, Object> map2 : list)
            {
                ExecListVO execListVO = new ExecListVO ();
                execListVO.setEmpIdExec ((String) map2.get ("empIdExec"));
                execListVO.setEmpNameExec ((String) map2.get ("empNameExec"));
                execListVO.setExecAction ((String) map2.get ("execAction"));
                execListVO.setExecNotes ((String) map2.get ("execNotes"));
                execListVO.setExecNumber ((Integer) map2.get ("execNumber"));
                execListVO.setExecResult ((String) map2.get ("execResult"));
                execListVO.setExecTime ((String) map2.get ("execTime"));
                execListVO.setOrgIdExec ((String) map2.get ("orgIdExec"));
                execListVO.setOrgNameExec ((String) map2.get ("orgNameExec"));
                execListVO.setPrescId ((Long) map2.get ("prescId"));
                execListVO.setTreatmentExecId ((Long) map2.get ("treatmentExecId"));
                execListVOs.add (execListVO);
            }
            vo.setExecList (execListVOs);
            pdsSupplyVOList.add (vo);
        }
        return m_translatorManager.translate (pdsSupplyVOList);

    }

    @Override
    public List <PdsSupplyDetailVO> findApplyAndDetailed (Map <String, Object> inmap)
    {
        s_logger.debug ("进入findApplyAndDetailed方法,入参:");
        List <PdsSupplyDetailVO> pdsSupplyDetailVOList = new ArrayList <PdsSupplyDetailVO> ();
        Map <String, Object> outMap = m_outPrescriptionService.findApplyAndDetailed (inmap);
        @SuppressWarnings ("unchecked")
        List <Map <String, Object>> result = (List <Map <String, Object>>) outMap.get ("result");

        for (Map <String, Object> map : result)
        {
            PdsSupplyDetailVO vo = new PdsSupplyDetailVO ();
            vo.setPrescNo ((String) inmap.get ("prescNo"));
            vo.setPhamName ((String) map.get ("phamName"));
            vo.setPhamSpec ((String) map.get ("phamSpec"));
            vo.setQtyUnit ((String) map.get ("baseUnit"));
            vo.setUsageCod ((String) map.get ("usageCode"));
            vo.setPhamType ((String) map.get ("phamType"));
            vo.setPhamStdCode ((String) map.get ("phamStdCode"));
            vo.setIsGroupOrder ((String) map.get ("isGroupOrder"));
            vo.setOrderId ((Long) map.get ("orderId"));
            vo.setFreqCode ((String) map.get ("freqCode"));
            vo.setCosts ((Double) map.get ("costs"));
            vo.setDurationUnit ((String) map.get ("durationUnit"));
            vo.setSortNumber ((Long) map.get ("sortNumber"));
            vo.setInfusionFlag ((Integer) map.get ("infusionFlag"));
            vo.setUnitQty ((Double) map.get ("unitQty"));
            vo.setFactor ((Double) map.get ("factor"));
            vo.setQtySum ((Double) map.get ("qtySum"));
            vo.setOrderItemDetailId (Long.parseLong (map.get ("orderItemDetailId") + ""));
            vo.setFreqTimeQty ((String) map.get ("freqTimeQty"));
            vo.setMedUnit ((String) map.get ("medUnit"));
            vo.setMedUnitName ((String) map.get ("medUnitName"));
            vo.setPerMedQty ((String) map.get ("perMedQty"));
            vo.setDurationQty ((Double) map.get ("durationQty"));
            vo.setTradeCosts ((Double) map.get ("tradeCosts"));
            vo.setPackageUnit ((String) map.get ("unitName"));
            vo.setIsSt ((String) map.get ("isSt"));
            vo.setOfficialDrugLevel ((String) map.get ("officialDrugLevel"));
            vo.setDosePerUnit (ServerUitls.getPhamDosePerUnit (vo.getPhamStdCode ()));
            pdsSupplyDetailVOList.add (vo);
        }
        return m_translatorManager.translate (pdsSupplyDetailVOList);
    }

    @Override
    public String checkPresc (Map <String, Object> map)
    {
        s_logger.info ("checkPresc (Map <String, Object> map = 【{}】", new Object[]
        { map });

        Map <String, Object> result = m_outpPrescExecService.addPrescExec (map);
        return (String) result.get ("msg");
    }

}
